Designs of Algorithm
(Computer systems, sub-systems and decomposition)
Structure, Flowchart & Pseudocode


Objectives : Student should be able to -


Q1. a)  Describe  Computer System. 

⇒  A computer system is made up of software, data, hardware, communications and people.

⇒  A computer system is a set of integrated devices that input, process, output, communicate and store data and information.

⇒  A computer system can be divided into a set of sub-systems.

⇒  Each sub-system can be further divided into sub-systems and so on until each sub-system just performs a single action.

b)  Describe what is meant by  Subsystem. 

⇒  A Sub-system is derived from a system and it is an integral part of a large system.

⇒  A Sub-system is a system in its own, but it normally can not provide a useful function by itself.

⇒  It must be integrated with other subsystems to make a system.

Q2. a)  Describe  Top-down design,  a modular approach to provide a solution to a problem.

✬  Top-down design is the decomposition of a computer system into sub-systems.

✬  It is the process of breaking down a system into a set of subsystems, then further breaking each sub-system down into a set of smaller sub-systems, until each sub-system just performs a single action.

b)  Give three  advantages  of ‘Top-down design’.

  1. Smaller sub-systems are easy to design and manage.
  2. Easy to test and debug errors.
  3. Several programmers can work independently to develop and test each subsystems of a large system at the same time.

c)  Give three  drawbacks  of ‘Top-down design’.

  1. Modules must be linked and testing must be carried out to make sure that the links work correctly.
  2. Programmers must ensure that the cross-referencing is done.
  3. Interfaces between modules must be planned.

Q3.  Any problem that uses a computer system for its solution needs to be decomposed into its component parts.

a)  Name and describe four component parts of any computer system.

1. Inputs : the data used by the system that needs to be entered while the system is active.
2. Process : the tasks that need to be performed using the input data and any other previously stored data.
3. Outputs : information that needs to be displayed or printed for the users of the system.
4. Storage : data that needs to be stored in files on an appropriate medium for use in the future.

b)  Identify three of the different items, other than software, that make up a computer system.

  1. Hardware
  2. Data
  3. Communication and people

►   Methods used to design and construct a solution to a problem

Q4. a)  Describe what is meant by  Algorithm. 

⇒  An algorithm is a set of instructions, telling computer what to do step by step to solve a problem.

⇒  Each steps are uniquely defined, either to take input, execute some process or output data.

⇒  The steps are normally in "sequence", "selection", "iteration" or a "case-type" statement.

⇒  The algorithm stops after execution of finite number of instructions.

b)  Describe how to design and construct an algorithm.

  1. Analyse the problem and make sure that you understood it properly.
  2. Determine what would be the -
  3. Break down the problem into sub-problems if it is complex.
  4. Write down all the steps needed to solve the problem sequentially from start to end.
  5. Determine what variables and constants need to be declared and initialized to setup the program.
  6. Determine the sequential statements and compound statements (like, selection and looping) need to be used.
  7. Construction your algorithm using either flowchart or pseudocode designing tools.
  8. Making sure that it can be easily read and understood by others. Use meaningful names for variables and constants.
  9. Use several sets of test data (normal, abnormal and boundary) and trace tables to find any errors in your algorithm.
  10. Debug the errors if found, and test your algorithm until it works perfectly to produce the desired output.

Q5.  Describe the following three methods of designing an algorithm to solve a problem.

1)  Structure diagram :

⇒  A diagram to show the hierarchical or ordered way of designing a system, by breaking down a system into its sub-systems to its lowest manageable levels in a tree like structure.

⇒  It is a top-down modular design tool, constructed using squares to represent different modules in the system, and lines that connect them. The lines represent the connection between activities and sub-activities as they are used in organization charts.

2)  Flowchart :

⇒  A Flowchart is a diagram that represents an algorithm, the work flow or process.

⇒  It shows the steps to be carried out to solve a problem.

⇒  It uses variety of boxes and arrows to show the process to be done and the direction of flow of data.

3)  Pseudocode :

⇒  Pseudocode is a method to describe the steps of an algorithm, using english words with common programming terms and mathematical operators that are set out to look like a program.

⇒  It is not bound by the strict syntax rules of any programming language to solve a problem.

⇒  While developing a real program to execute, pseudocode can then be coded into any programming language of choice.

  Structure diagram

Q6.  A Satellite Navigation System allows the user to enter the destination details, either a new destination or chosen from previously saved destinations. The satellite navigation system will then output directions to the destination in the form of either a visual map or a list of directions.

Draw a structured diagram for the navigation system.

Structure diagram of Satellite Navigation System

Q7.  An Alarm app for a smart phone allows to the set the alarm time, checks and sounds the alarm for 2 minutes if the current time is equal to the set time.

Draw a structured diagram for the Alarm app computer system.

Structure diagram of Alarm app

  Flow-chart

Q8. a)  Draw and describe the use of four  flowchart symbols. 

Name Symbol Function

Terminator
( Start / End )

terminator An oval represents a Start or End of an algorithm.

Input / Output

input-output A parallelogram represent input or output.

Process

process A rectangle represents a process. It is used for arithmetical operation and data-manipulations.

Decision

Decision A diamond represents a decision to take for the direction of the flow of data.

Arrow

Arrow Arrows shows the logical flow of process and data.

Subroutine symbol

Subroutine Denotes a pre-defined subprogram that performs a specific task.
It could be described in more detail on separate flowchart.

b)  Describe the purpose of flow lines in a flowchart.

⇒  Flow lines uses arrows to show the direction of flow of process and data.

Q9.  Draw a flowchart algorithm which takes a positive number as input, determines whether the number is even or odd and output the result.

The algorithm make use of the MOD function which divides one integer by another and gives the remainder, like MOD(5, 2) = 1 (i.e. 5 divided by 2 gives 2 remainder 1).

flowchart_even_odd

Q10.  There are two different ways of drawing a flowchart to input 20 numbers and output the total.

Draw flowcharts for pre-condition loop and the post-condition loop.

a)  Pre-condition loop :

Flowchart 7.2.1

b)  Post-condition loop :

Flowchart 7.2.2

Q11.  Give  difference  between  structure diagram  and  flowchart. 

Structure diagram
Flowchart
It represents the software architecture by breaking down a problem into more details with the help of modules. It represents the flow of control in an algorithm with graphical design using variety of boxes and arrows.
Easier to identify modules or sub-modules in a program. Difficult to recognize and identify different modules.
Represents the structure in hierarchical method. Represents the flow of controls in sequential structure.
It is quite complex to understand and design. Flowchart is easier to construct and understand.

  Pseudocode

Q12.  Describe the function of each of the following types of pseudocode statement and give an example in pseudocode for the use of each one.

1)  Assignment statement :

⇒  The variable on the left of the operator is assigned a value or an expression using mathematical operators.

Example : Total 0
Variable 'Total' stores the value '0'.
  Total Total + Num
Variable 'Total' stores the value 'Total + Num'.

2)  Selection (conditional) statement :

It is used, when different actions need to be performed by an algorithm according to the values of the variables.

There are two types of conditional statements :

(i)  "IF ... THEN ... ELSE ... ENDIF" Conditional Selection statement :

⇒  Allows to test the given condition and execute the instructions if it is true or false, based on “relational operator” (like, <, >, =, AND, OR, NOT).

Example : IF (Age < 18) THEN
OUTPUT "Child"
ELSE
OUTPUT "Adult"
ENDIF

(ii)  "CASE ... OF ... OTHERWISE ... ENDCASE" Conditional Switching statement :

⇒  Allows to make choice and execute the instructions if it is true, it is based on “equality operator ( = )”.

Example : CASE Grade OF
"A" : OUTPUT "Excellent"
"B" : OUTPUT "Good"
"C" : OUTPUT "Average"
OTHERWISE
OUTPUT "Improvement is needed"
ENDCASE

3)  Iterative or Looping statement :

⇒  Iteration is the term given to the repetition of a block of instructions (code) within a computer program for a given number of repetitions or continuously either for as long as a condition continues to be true or until a condition becomes true.

⇒  When a block of code is executed out again, it is called an iteration.
When a cycle of instructions is carried out in a repeated manner, it is called a loop.

⇒  Iteration allows programmers to simplify a program and make it more efficient. Instead of writing out the same lines of code again and again, a programmer can write a section of code once, and ask the program to execute the same block of code repeatedly until it is no longer needed.

There are three types of Looping structures :

(i)  "FOR ... TO ... NEXT" is called Counting loop :

☛ The FOR ... NEXT loop is used to execute the block of instructions repeatedly for a fixed number of times.

In the following example, the counter variable "X" starts at 1 and its value is increamented by 1 every time the code repeats until it reaches a value of 10, to terminate the loop.

Example : FOR Count = 1 TO 10
INPUT Num
Total = Total + Num
NEXT Count

(ii)  "WHILE ... DO ... ENDWHILE" is a pre-conditional loop :

☛ The WHILE ... DO loop. executes the block of code and continue to loop or repeat only if the condition is True.
It checks for the condition before executing the block of code.
The loop may not execute the block of code atleast once, if the condition is False.

The following example will continue to take user input till the value of Count is less than 10 (ten times : for Count = 0 to 9).

Example : Count 0
WHILE Count < 10 DO
INPUT Num
Count = Count + 1
ENDWHILE

(iii)  "REPEAT ... UNTIL" is a post-conditional loop :

☛ The REPEAT.. UNTIL loop, executes the block of code and continue to repeat until the condition is True (repeats only if the condition is False). It checks for the condition after executing the block of code. It need to be executed at least once.

The following example will continue to take user input until the user inputs a value between 0 and 50 inclusive.
This loop structure is used for Range checks validation.

Example : REPEAT
INPUT Num
IF (Num < 0 ORNum < 50 THEN
PRINT "Invalid data, please re-enter the number."
ENDIF
UNTIL (Num >= 0 AND Num <= 50)

Q13. a)  Write an algorithm using pseudocode to input ten numbers. Calculate and output the average.

Total = 0
FOR Count = 1 TO 10
INPUT "Enter the number :", Num
Total = Total + Num
NEXT Count
Avg = Total / 10
OUTPUT "The sum of the input numbers is ", Avg

b)  Write an algorithm using flowchart to input ten numbers. Calculate and output the average.

Flowchart 7.2.3

Q14. a)  What is the  similarity  between  Flowchart  and  Pseudocode .

⇒  Flowchart and Pseudocode are two different tools to represent an Algorithm that illustrates a solution to a given problem and helps to develop a software.

b)  What are the  differences  between  Flowchart  and  Pseudocode .

⇒  Flowchart is a pictorial representation of an algorithm, while pseudocode is an informal high-level description of an algorithm.

c)  How a programmer could decide whether to use flowchart or pseudocode algorithm? Give its  advantages  and  limitation. 

⇒  The type of algorithm to use is choosen after analyzing the time complexity and space complexity of an algorithm.

⇒  Flowcharts has the advantage of being easy to understand the logic of given problem compared to Pseudocode. It is used in programming to show the steps to write a program.
But has a limitation of time and space complexity because it is a diagrammatic representation with various symbols to illustrate a solution to a problem.

⇒  Pseudocode has the advantage of being independent of a specific language.

⇒  Pseudocode also has the advantage of being easier to write because you don't have to satisfy the restrictions of the syntax of a language, so it is used when you want to express an idea quickly.

Q15.  Describe how to check the  Effectiveness  of an algorithm or solution.

In order to consider the effectiveness of a given solution as the following questions -

  1. Does the solution work for all sets of test data ?
  2. Does the solution have any unnecessary process that are never used ?
  3. Are any actions repeated unnecessarily ?
  4. Can the solution be simplified and still work as well ?

Q16.  What is meant by  Evaluation  of an algorithm ?

⇒  Evaluation is the process that allows us to make sure our algorithm does the job it has been designed to do and to think about how it could be improved.

Once algorithm is written, it should be checked to make sure :

If an algorithm meets these four criteria it is likely to work well. The algorithm can then be programmed.

Q17.  Algorithms should be evaludated using the following criteria:

a)  How do you  analyze the efficiency  of an algorithm.

An algorithm's efficiency can be judged in terms of :

⇒  Speed : How quick the algorithm produces the required output.

⇒  Complexity of algorithm : How many lines of code the algorithm contains.

b)  How do you  check the correctness  of an algorithm.

Although an algorithm is expected to produce the correct outputs, correctness can still be measured in terms of :

⇒  Accuracy : How many decimal places produce output with greater accuracy (e.g. more decimal places).

⇒  Range : Will the algorithm work with the complete range of inputs? Or can it only deal with positive numbers, whole numbers, numbers below 1 million, etc.

⇒  Reliability : Will the algorithm always produce correct output within the range that it is designed to work? Or are there values which it will not accept (e.g. zero).

c)  How do you  check the appropriateness  of an algorithm.

Appropriateness can be measured in terms of :

⇒  Length : If the problem is simple then a short algorithm would normally be expected.

⇒  Speed : If the output need to be generated quickly, then the algorithm must be able to generate output quick enough.

⇒  Memory requirements : An algorithm should use a minimum possible memory.




* * * * * * * * *
* * * * * *
* * *
*